﻿using ConsoleApplication.WebAPI;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    [HotUpdate]
    [Description("新品规则表保存后新增物料")]
    public class CreateMaterial : AbstractDynamicFormPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.Equals("tbSplitApprove")|| e.BarItemKey.Equals("tbApprove"))
            {
                string F_WNEG_Specification = "", F_WNEG_Group="", F_WNEG_ErpClsID="", FName="",
                    F_WNEG_BaseUnitId ="", F_WNEG__WLLZ_Text="", F_WNEG_LargeText="",
                    F_WNEG__QZPV_Price="", F_WNEG__QZPV_Text1="", F_WNEG__WLLZ_TaxRateId="", F_WNEG__QZPV_Price1 = "",
                    F_WNEG_Assistant="", F_WNEG_TaxRateId1="", F_WNEG_Decimal="", F_WNEG_Decimal1="", F_WNEG_Decimal2="", F_WNEG_Decimal3="",
                    F_WNEG_LENGTH="", F_WNEG_WIDTH="", F_WNEG_HEIGHT="", F_WNEG__WLLZ_Qty1="", F_WNEG__WLLZ_Qty2="", F_WNEG__WLLZ_Qty="",
                    F_WNEG__QZPV_LENGTH="", F_WNEG__QZPV_WIDTH="", F_WNEG__QZPV_HEIGHT="", F_WNEG__QZPV_VOLUME="", F_WNEG__QZPV_Qty="", F_WNEG_Text1="", FMATERIALID="",
                    MESSAGE="",F_WNEG_Base2="", F_WNEG_Text5="", F_WNEG__QZPV_Remark="", FSupplierId="", ID="", F_WNEG_Image1="", FCreatorld="";
                //if (this.Model.GetValue("FMaterialId") != null)
                //{
                //    this.View.ShowErrMessage("已存在物料编码，请清空再新增保存！");
                //}
                //else
                //{
                    ///基本信息
                    FName = Convert.ToString(this.Model.GetValue("FName"));
                    F_WNEG_Text1 = Convert.ToString(this.Model.GetValue("F_WNEG_Text1"));
                    string SQL0 = "/*dialect*/" + string.Format(@"select FMATERIALID from T_BD_MATERIAL where fnumber='{0}'
                    ", F_WNEG_Text1);
                    DynamicObjectCollection DynamData0 = DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                    if(DynamData0.Count>0)
                    {
                    FMATERIALID = Convert.ToString(DynamData0[0]["FMATERIALID"]);
                    MESSAGE = "修改成功！";
                    }
                    else
                    {
                    FMATERIALID = "0";
                    MESSAGE = "新增成功！";
                    }
                    //F_WNEG_Image1 =Convert.ToString(this.Model.GetValue("F_WNEG_Image1"));
                    F_WNEG_Specification = Convert.ToString(this.Model.GetValue("F_WNEG_Specification"));
                    F_WNEG_Text5 = Convert.ToString(this.Model.GetValue("F_WNEG_Text5"));
                    F_WNEG__QZPV_Remark = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_Remark"));
                    if (this.Model.GetValue("F_WNEG_Group") != null)
                    {
                        F_WNEG_Group = Convert.ToString((this.Model.GetValue("F_WNEG_Group") as DynamicObject)["Number"]);
                    }
                    if (this.Model.GetValue("F_WNEG_Base2") != null)
                    {
                        F_WNEG_Base2 = Convert.ToString((this.Model.GetValue("F_WNEG_Base2") as DynamicObject)["Number"]);
                    }
                    F_WNEG_ErpClsID = Convert.ToString(this.Model.GetValue("F_WNEG_ErpClsID"));
                    if (this.Model.GetValue("F_WNEG_BaseUnitId") != null)
                    {
                        F_WNEG_BaseUnitId = Convert.ToString((this.Model.GetValue("F_WNEG_BaseUnitId") as DynamicObject)["Number"]);
                    }
                    F_WNEG__WLLZ_Text = Convert.ToString(this.Model.GetValue("F_WNEG__WLLZ_Text"));
                    F_WNEG_LargeText = Convert.ToString(this.Model.GetValue("F_WNEG_LargeText"));

                    ///财务信息
                    F_WNEG__QZPV_Price = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_Price"));
                    F_WNEG__QZPV_Text1 = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_Text1"));
                    if (this.Model.GetValue("F_WNEG__WLLZ_TaxRateId") != null)
                    {
                        F_WNEG__WLLZ_TaxRateId = Convert.ToString((this.Model.GetValue("F_WNEG__WLLZ_TaxRateId") as DynamicObject)["Number"]);
                    }
                    F_WNEG__QZPV_Price1 = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_Price1"));
                    if (this.Model.GetValue("F_WNEG_Assistant") != null)
                    {
                        F_WNEG_Assistant = Convert.ToString((this.Model.GetValue("F_WNEG_Assistant") as DynamicObject)["Number"]);
                    }
                    if (this.Model.GetValue("F_WNEG_TaxRateId1") != null)
                    {
                        F_WNEG_TaxRateId1 = Convert.ToString((this.Model.GetValue("F_WNEG_TaxRateId1") as DynamicObject)["Number"]);
                    }
                    if (this.Model.GetValue("FSupplierId") != null)
                    {
                        FSupplierId = Convert.ToString((this.Model.GetValue("FSupplierId") as DynamicObject)["Number"]);
                    }
                    F_WNEG_Decimal = Convert.ToString(this.Model.GetValue("F_WNEG_Decimal"));
                    F_WNEG_Decimal1 = Convert.ToString(this.Model.GetValue("F_WNEG_Decimal1"));
                    F_WNEG_Decimal2 = Convert.ToString(this.Model.GetValue("F_WNEG_Decimal2"));
                    F_WNEG_Decimal3 = Convert.ToString(this.Model.GetValue("F_WNEG_Decimal3"));
                    ///单个产品包装尺寸
                    F_WNEG_LENGTH = Convert.ToString(this.Model.GetValue("F_WNEG_LENGTH"));
                    F_WNEG_WIDTH = Convert.ToString(this.Model.GetValue("F_WNEG_WIDTH"));
                    F_WNEG_HEIGHT = Convert.ToString(this.Model.GetValue("F_WNEG_HEIGHT"));
                    F_WNEG__WLLZ_Qty1 = Convert.ToString(this.Model.GetValue("F_WNEG__WLLZ_Qty1"));
                    F_WNEG__WLLZ_Qty2 = Convert.ToString(this.Model.GetValue("F_WNEG__WLLZ_Qty2"));
                    F_WNEG__WLLZ_Qty = Convert.ToString(this.Model.GetValue("F_WNEG__WLLZ_Qty"));
                    ///大外箱尺寸
                    F_WNEG__QZPV_LENGTH = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_LENGTH"));
                    F_WNEG__QZPV_WIDTH = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_WIDTH"));
                    F_WNEG__QZPV_HEIGHT = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_HEIGHT"));
                    F_WNEG__QZPV_VOLUME = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_VOLUME"));
                    F_WNEG__QZPV_Qty = Convert.ToString(this.Model.GetValue("F_WNEG__QZPV_Qty"));
                    ID= this.View.Model.DataObject["Id"].ToString();
                FCreatorld = Convert.ToString((this.Model.GetValue("FCreatorld") as DynamicObject)["Name"]);
                //string SQL1 = "/*dialect*/" + string.Format(@"select F_WNEG_Image1 from WNEG_t_Cust100018 where FID='{0}'
                //    ", ID);
                //DynamicObjectCollection DynamData1 = DBUtils.ExecuteDynamicObject(this.Context, SQL1);
                //if(DynamData1.Count>0)
                //{
                //     F_WNEG_Image1 = Convert.ToString(DynamData1[0]["F_WNEG_Image1"]);
                //}
                    var result = InvokeHelper.Login();
                    var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();
                    if (iResult == 1)
                    {
                        JObject json = new JObject();
                        json.Add("IsDeleteEntry", "true");
                        json.Add("ValidateFlag", "true");
                        json.Add("IsAutoAdjustField", "true");
                        json.Add("IsAutoSubmitAndAudit", "true");
                        JObject item = new JObject();
                        item.Add("FMATERIALID", FMATERIALID);
                        JObject Obj = new JObject();
                        Obj.Add("FNumber", "100");
                        item.Add("FCreateOrgId", Obj);
                        Obj = new JObject();
                        Obj.Add("FNumber", "100");
                        item.Add("FUseOrgId", Obj);
                        item.Add("FNumber", F_WNEG_Text1);
                        item.Add("FName", FName);
                        item.Add("FSpecification", F_WNEG_Specification);
                        item.Add("F_WNEG_Text5", F_WNEG_Text5);
                        item.Add("F_QZPV_Remark", F_WNEG__QZPV_Remark);
                        Obj = new JObject();
                        Obj.Add("FNumber", F_WNEG_Group);
                        item.Add("FMaterialGroup", Obj);
                        Obj = new JObject();
                        Obj.Add("FNumber", F_WNEG_Base2);
                        item.Add("F_WNEG_Base", Obj);
                        item.Add("F_WLLZ_Text", F_WNEG__WLLZ_Text);
                        item.Add("F_WNEG_LargeText", F_WNEG_LargeText);
                        item.Add("F_QZPV_Price", F_WNEG__QZPV_Price);
                        item.Add("F_QZPV_Text1", F_WNEG__QZPV_Text1);
                        Obj = new JObject();
                        Obj.Add("FNumber", F_WNEG__WLLZ_TaxRateId);
                        item.Add("F_WLLZ_TaxRateId", Obj);
                        item.Add("F_QZPV_Price1", F_WNEG__QZPV_Price1);
                        Obj = new JObject();
                        Obj.Add("FNumber", F_WNEG_Assistant);
                        item.Add("F_WNEG_Assistant", Obj);
                        Obj = new JObject();
                        Obj.Add("FNumber", F_WNEG_TaxRateId1);
                        item.Add("FTaxRateId1", Obj);
                        item.Add("F_WNEG_Decimal", F_WNEG_Decimal);
                        item.Add("F_WNEG_Decimal1", F_WNEG_Decimal1);
                        item.Add("F_WNEG_Decimal2", F_WNEG_Decimal2);
                        item.Add("F_WNEG_Decimal3", F_WNEG_Decimal3);
                   
                        item.Add("F_WLLZ_Qty2", F_WNEG__WLLZ_Qty2);
                        item.Add("F_WLLZ_Qty", F_WNEG__WLLZ_Qty);

                        item.Add("F_QZPV_Qty", F_WNEG__QZPV_Qty);
                        //item.Add("F_WNEG_Text15", "1222222");

                        JObject SubHeadEntity = new JObject();
                        SubHeadEntity.Add("FErpClsID", F_WNEG_ErpClsID);
                        SubHeadEntity.Add("FLENGTH", F_WNEG_LENGTH);
                        SubHeadEntity.Add("FWIDTH", F_WNEG_WIDTH);
                        SubHeadEntity.Add("FHEIGHT", F_WNEG_HEIGHT);
                        SubHeadEntity.Add("F_WLLZ_Qty1", F_WNEG__WLLZ_Qty1);
                        SubHeadEntity.Add("F_QZPV_LENGTH", F_WNEG__QZPV_LENGTH);
                        SubHeadEntity.Add("F_QZPV_WIDTH", F_WNEG__QZPV_WIDTH);
                        SubHeadEntity.Add("F_QZPV_HEIGHT", F_WNEG__QZPV_HEIGHT);
                        SubHeadEntity.Add("F_QZPV_VOLUME", F_WNEG__QZPV_VOLUME);
                        Obj = new JObject();
                        Obj.Add("FNumber", F_WNEG_BaseUnitId);
                        SubHeadEntity.Add("FBaseUnitId", Obj);

                        item.Add("SubHeadEntity", SubHeadEntity);
                        JObject SubHeadEntity3 = new JObject();
                        Obj = new JObject();
                        Obj.Add("FNumber", FSupplierId);
                        SubHeadEntity3.Add("FDefaultVendor", Obj);
                        item.Add("SubHeadEntity3", SubHeadEntity3);
                        json.Add("Model", item);
                        result = InvokeHelper.Save("BD_MATERIAL", json.ToString());
                        var result0 = JObject.Parse(result);
                        var IsSuccess = result0["Result"]["ResponseStatus"]["IsSuccess"];
                        var SuccessEntitysId = result0["Result"]["ResponseStatus"]["SuccessEntitys"][0]["Id"];
                        if (IsSuccess.ToString().Equals("True"))
                        {

                        string SQL2 = "/*dialect*/" + string.Format(@"UPDATE  t SET t.FIMAGE=t1.F_WNEG_Image1,T.FIMGSTORAGETYPE='A',T.F_WNEG_Text17='{1}' from T_BD_MATERIAL t
                        left join WNEG_t_Cust100018 t1 on t.fnumber=t1.F_WNEG_Text1
                        WHERE t.FMATERIALID='{0}'
                        ", SuccessEntitysId, "99999");
                        DBUtils.ExecuteDynamicObject(this.Context, SQL2);
                        this.View.ShowMessage(MESSAGE);
                        }
                        else
                        {
                        var Errors = result0["Result"]["ResponseStatus"]["Errors"];
                        this.View.ShowErrMessage("新增或修改失败！'"+ Errors + "'");
                        }
                    }

                        
                //}
            }
        }
    }
}
